package com.wh.hotel.service.strategy.time;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.wh.hotel.po.ValidTimePo;
import com.wh.hotel.util.DateWhUtil;
import com.wh.hotel.vo.HotelPriceStrategyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author luweije
 */
@Slf4j
@Component
public class PreWeekValidTime extends ValidTimeAbstract {

    @Override
    public boolean isHit(Date cur, HotelPriceStrategyVo strategyVo) {
        ValidTimePo validTimePo = strategyVo.getValidTimePo();
        int weekNum = DateWhUtil.getWeekNum(cur);
        return validTimePo.getWeekdayList().contains(weekNum);
    }

    @Override
    public List<Date> calculateValidTime(HotelPriceStrategyVo strategyVo, Date startDt, Date endDt) {
        ValidTimePo validTimePo = strategyVo.getValidTimePo();
        Set<Integer> weekdaySet = Sets.newHashSet(validTimePo.getWeekdayList());
        Date cur = startDt;
        List<Date> result = Lists.newArrayList();
        while (DateWhUtil.ge(endDt, cur)) {
            int weekNum = DateWhUtil.getWeekNum(cur);
            if (weekdaySet.contains(weekNum)) {
                result.add(cur);
            }
            cur = DateWhUtil.addDays(cur, 1);
        }
        return result;
    }
}
